package top.luoqiz.jenkins.config.sms.store;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author luoqiz
 */
@Slf4j
public class InMemoryVerificationCodeStore implements VerificationCodeStore {

    ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap();

    @Override
    public boolean save(String mobile, String code) {
        log.info("向 %s 手机号发送验证码：%s", mobile, code);
        concurrentHashMap.put(mobile, code.trim());
        return true;
    }

    @Override
    public boolean verification(String mobile, String code) {
        String storeCode = concurrentHashMap.get(mobile);
        if (code.trim().equals(storeCode)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delete(String mobile) {
        concurrentHashMap.remove(mobile);
        return true;
    }

    @Override
    public boolean clear() {
        concurrentHashMap.clear();
        return true;
    }
}
